﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SMSTemplate.aspx.cs" Inherits="CRMWeb.Pages.Admin.SMSTemplate" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>SMS Template</title>
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="/Css/CrmStyle.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/EasyUI/jquery.easyui.min.js"></script>
</head>
<body>
    <form id="frmSMSTemplate" method="post">
        <div id="pnlSMS" title=" SMS Template" class="easyui-panel" style="width: auto; height: auto">
            <div class="fitem" style="text-align: left">
                <a id="lnknewSMSTemplate" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newSMSTemplate()">New</a>&nbsp;
                <a id="lnkdeleteSMSTemplate" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="deleteSMSTemplate()">Delete</a>
            </div>
            <div class="fitem">
                <label style="width: 120px;margin-left:20px">Select Template:</label>
                <span id="spnddlSMSTemplate">

                    <select class="easyui-combotree" id="ddlSMSTemplate" style="width: 430px;"
                        data-options="url:'/WebServices/SMSTemplate.ashx?method=DisplayTemplateNode',required:true, onSelect:selectSMSTemplate">
                    </select>
                </span>
                <input id="txtSMSTemplate" class="easyui-validatebox" required="true" />
               
            </div>
            <div class="fitem">
                <label style="width: 120px;margin-left:20px">Field:</label>
                <input class="easyui-combobox" id="ddlField" name="Field" style="width: 300px;" data-options="panelHeight:'300',url:'/WebServices/SMSTemplate.ashx?method=getColumnName1',
				valueField:'COLUMN_NAME',
				textField:'COLUMN_NAME',
                onSelect: function(rec){ 
                    var caretPos = document.getElementById('txtTemplateSMSFormat').selectionStart;
                    var textAreaTxt = jQuery('#txtTemplateSMSFormat').val();
                    var txtToAdd = '%'+rec.COLUMN_NAME+'%';
                    jQuery('#txtTemplateSMSFormat').val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
                }" />
            </div>
            <div class="fitem">
                <span id="spnCategory" style="display:none">
                    <label style="width: 120px; margin-left: 20px">Category :</label>
                    <select class="easyui-combotree" id="cmbCategory" style="width: 450px;"
                        data-options="url:'/WebServices/SMSTemplate.ashx?method=DisplayCategoryList',required:true">
                    </select>
                </span>
            </div>
            <div class="fitem">
                <label style="width: 120px;margin-left:20px">Operator :</label>
                <select id="cmbOperatorSMS" data-options="panelHeight:'auto',onSelect: function(rec){
                    var caretPos = document.getElementById('txtTemplateSMSFormat').selectionStart;
                    var textAreaTxt = jQuery('#txtTemplateSMSFormat').val();
                    var txtToAdd = $('#cmbOperatorSMS').combogrid('getValue');
                    jQuery('#txtTemplateSMSFormat').val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
                    }"
                    class="easyui-combobox" name="Operator" style="width: 110px;">
                    <option value="%OperatorName%">Operator Name</option>
                </select>
            </div>
            <input id="txtOprSMS" class="easyui-validatebox" required="true" hidden="true" />

            <div id="divSmsText" class="fitem">
                <label style="vertical-align: top; width: 120px;margin-left:20px">Description:</label>
                <textarea id="txtTemplateSMSFormat" name="SMSTemplateDescription" class="easyui-validatebox" required="true" style="width: 70%; height: 230px"></textarea>
            </div>
            <div style="text-align: right; margin-right: 10%">
                <a id="lnkcancelSMSTemplate" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="cancelSMSTemplate()"> Cancel </a>
                <a href="javascript:void(0)"  class="easyui-linkbutton" iconcls="icon-ok" onclick="SaveSMSTemplate()"> Save </a>
            </div>
        </div>
        
         
    </form>

    <script type="text/javascript">
        $(document).ready(function () {

            $('#ddlSMSTemplate').combotree({
                onClick: function (node) {
                    if (!$('#ddlSMSTemplate').tree('isLeaf', node.target)) {
                        $('#ddlSMSTemplate').combotree('setValues', '');
                        var t = $('#ddlSMSTemplate').combotree('tree');
                        t.tree('toggle', node.target);
                        var p = $('#ddlSMSTemplate').combotree('panel');
                        p.panel('open');
                    }
                }
            });
            $('#cmbCategory').combotree({
                onClick: function (node) {
                    if (!$('#cmbCategory').tree('isLeaf', node.target)) {
                        $('#cmbCategory').combotree('setValues', '');
                        var t = $('#cmbCategory').combotree('tree');
                        t.tree('toggle', node.target);
                        var p = $('#cmbCategory').combotree('panel');
                        p.panel('open');
                    }
                }
            });

            $('#txtSMSTemplate').hide();
            $('#lnkcancelSMSTemplate').hide();
            $('#pnlSMS').panel('resize', {
                width: window.innerWidth,
                height: window.innerHeight
            });
            $('#ddlField').combobox('textbox').bind('click', function () {
                $('#ddlField').combobox('showPanel');
            });
            $('#ddlSMSTemplate').combobox('textbox').bind('click', function () {
                $('#ddlSMSTemplate').combobox('showPanel');
            }); 
            $('#cmbCategory').combobox('textbox').bind('click', function () {
                $('#cmbCategory').combobox('showPanel');
            });

            $('#lnkdeleteSMSTemplate').hide();
        });
        $(document).keydown(function (event) {
            if ((event.which == 83 || event.which == 115) && (event.ctrlKey || event.metaKey) || (event.which == 19)) {
                event.preventDefault();
                // do stuff - ctrl + s
                SaveSMSTemplate();
                return false;
            }
            return true;
        });
        window.onresize = function () {
            $('#pnlSMS').panel('resize', {
                width: window.innerWidth,
                height: window.innerHeight
            });
            $('#txtTemplateSMSFormat').css('height', (window.innerHeight / 1.5)-50);
        }
        function selectSMSTemplate(node) {

            var currentTemplateId = node.id;
            if (currentTemplateId == -1) {
                $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
            }
            else {
                $('#lnkdeleteSMSTemplate').show();
                $.ajax({
                    url: "/WebServices/SMSTemplate.ashx?method=GetSMSTemplateContentById",
                    contentType: "application/json; charset=utf-8",
                    data: { smsTemplateId: currentTemplateId },
                    success: function (data) {
                        $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {
                            $('#cmbCategory').combotree('setValue', result.CategoryId);
                            $('#txtTemplateSMSFormat').val(result.TemplateFormat);
                        });
                    }
                });
            }
        }
        function newSMSTemplate() {
            $('#pnlSMS').panel('setTitle', 'Create New SMS Template');
            $('#txtSMSTemplate').val('');
            $('#txtTemplateSMSFormat').val('');
            $('#ddlSMSTemplate').combotree('reload');
            $('#ddlSMSTemplate').combotree('setValue', '');
            $('#cmbCategory').combotree('setValue', '');
            $('#ddlField').combobox('setValue', '');
            $('#txtSMSTemplate').show();
            $('#spnddlSMSTemplate').hide();
            $('#spnCategory').show();
            $('#lnkdeleteSMSTemplate').hide();
            $('#lnkcancelSMSTemplate').show();
            $('#lnknewSMSTemplate').hide();
        }
        function cancelSMSTemplate() {
            $('#pnlSMS').panel('setTitle', 'SMS Template');
            $('#txtSMSTemplate').val('');
            $('#txtTemplateSMSFormat').val('');
            $('#ddlSMSTemplate').combotree('reload');
            $('#ddlSMSTemplate').combotree('setValue', '');
            $('#cmbCategory').combotree('setValue', '');
            $('#ddlField').combobox('setValue', '');
            $('#txtSMSTemplate').hide();
            $('#spnddlSMSTemplate').show();
            $('#spnCategory').hide();
             
            $('#lnkcancelSMSTemplate').hide();
            $('#lnknewSMSTemplate').show();
            $('#lnkdeleteSMSTemplate').hide();
        }
        function deleteSMSTemplate() {
            var t = $('#ddlSMSTemplate').combotree('tree');	// get the tree object
            var templatNode = t.tree('getSelected');

            var SMSTemplateID = templatNode.id;
            if (SMSTemplateID != '') {
                $.ajax({
                    url: '/WebServices/SMSTemplate.ashx',
                    type: 'POST',
                    data: {
                        method: 'SMSTemplateDelete',
                        args: { SMSTemplateID: SMSTemplateID }
                    },
                    success: function (data) {
                        $.messager.alert('Morgan CRM', 'SMS Template Successfully Deleted');
                        cancelSMSTemplate();
                        
                    }
                });
            }
            else {
                $.messager.alert('Morgan CRM', 'Please select value to delete');
            }
        }
        function SaveSMSTemplate() {
            var SMSTemplateID ='';
            var tr = $('#cmbCategory').combotree('tree');	// get the tree object
            var Categorynode = tr.tree('getSelected');            // get selected node

            var t = $('#ddlSMSTemplate').combotree('tree');	// get the tree object
            var templatNode = t.tree('getSelected');
            if (templatNode != null)
                SMSTemplateID = templatNode.id;
            var SMSTemplate = $('#txtSMSTemplate').val();
            var SMSTemplateFormat = $('#txtTemplateSMSFormat').val();

            if (SMSTemplateID != '') {
                if (SMSTemplateFormat != '') {
                    $.ajax({
                        url: '/WebServices/SMSTemplate.ashx',
                        type: 'POST',
                        data: {
                            method: 'SMSTemplateUpdate',
                            args: { SMSTemplateID: SMSTemplateID, TemplateFormat: SMSTemplateFormat,CategoryID:Categorynode.id }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $.messager.alert('Morgan CRM', 'SMS Template Successfully Updated');
                                cancelSMSTemplate();
                            }
                            else
                                $.messager.alert('Morgan CRM', 'SMSTemplate already exist !');
                        }
                    });
                }
            }
            else {
                if (SMSTemplate != '' && SMSTemplateFormat != '') {
                    $.ajax({
                        url: '/WebServices/SMSTemplate.ashx',
                        type: 'POST',
                        data: {
                            method: 'SMSTemplateAdd',
                            args: { SMSTemplate: SMSTemplate, TemplateFormat: SMSTemplateFormat,CategoryID:Categorynode.id }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $.messager.alert('Morgan CRM', 'SMS Template added successfully ');
                                cancelSMSTemplate();
                               }
                            else
                                $.messager.alert('Morgan CRM', 'SMS Template already exist !');
                        }
                    });
                }
            }
        }
    </script>
</body>
</html>
